Expand description
tia; trait, impl accessors | automatic
This is a syntax sugar proc-macro crate for trait
, impl
accessors patterns. tia
generate to an accessor impl
s of an indivisual trait
s for any struct
|enun
|union
s.
Basic Example
Cargo.toml:
[dependencies]
tia = "*"
main.rs:
use tia::Tia; // 1. use
#[derive(Tia)] // 2. derive
#[tia(rg)] // 3. tia directives
struct MyStruct
{
foo: i32,
bar: String
}
fn main()
{
let mys = MyStruct {
foo: 123,
bar: "Hello".into()
};
let foo = mys.get_foo(); // <-- 4. !! generated by tia automatically !!
let bar = mys.get_bar(); // <-- 5. !! generated by tia automatically !!
println!("foo={} bar={}", foo, bar);
}
cargo run
:
foo=123 bar=Hello
with trait
s;
use tia::Tia;
trait FooGettable<T>
{
fn get_foo(&self) -> T;
}
trait Fruit
{
fn get_bar(&self) -> &String;
}
trait Sushi
{
fn tuna(&self) -> u8;
fn avocado(&mut self, v: u8);
}
// include!(".tia/MyStruct.rs");
#[derive(Tia, Debug, Default)] // derive
struct MyStruct
{
#[tia(s, "FooGettable<i32>", g)]
foo: i32,
#[tia("Fruit", rg, "", rsi)]
bar: String,
#[tia("Sushi",g*="tuna",s*="avocado")] // <- `g` and `s`: Sushi trait
baz: u8
}
/// Build ok = Test ok
fn main()
{
let mut mys = MyStruct::default();
mys.set_foo(123);
mys.set_bar("meow");
let foo_gettable = &mys as &dyn FooGettable<i32>;
let fruit = &mys as &dyn Fruit;
println!("{}, {}", foo_gettable.get_foo(), fruit.get_bar());
let sushi = &mut mys as &mut dyn Sushi;
sushi.avocado(32);
println!("{}", sushi.tuna());
}
cargo run
:
123, meow
32
More details and examples are exists in the README.md and examples/ and tests/.